package com.qc.wintrax.adapter;

import android.content.Context;
import android.widget.SectionIndexer;

import com.qc.wintrax.R;
import com.qc.wintrax.model.EventInfo;

import java.util.List;

import static android.R.id.list;

/**
 * Created by Administrator on 2016/8/24.
 */

public class EventInfoAdapter extends CommonAdapter<EventInfo>  implements SectionIndexer{
    private List<EventInfo> eventInfoList;
    private Context mContext;

    public EventInfoAdapter(Context context, List<EventInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.eventInfoList = datas;
        this.mContext = context;
    }

    public void updateList(List<EventInfo> eventInfoList){
        this.eventInfoList = eventInfoList;
        notifyDataSetChanged();
    }

    @Override
    public void convert(ViewHolder holder, EventInfo eventInfo) {
        holder.setText(R.id.event_title,eventInfo.getTitle());
        holder.setText(R.id.publisher_txt,eventInfo.getPublisher());
        holder.setText(R.id.time_txt,eventInfo.getTime());
        holder.setImageResource(R.id.img_backview,eventInfo.getImgId());
    }

    @Override
    public Object[] getSections() {
        return null;
    }

    @Override
    public int getPositionForSection(int section) {
        for (int i = 0; i < eventInfoList.size(); i++) {
            String sortStr = eventInfoList.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int i) {
        return eventInfoList.get(i).getSortLetters().charAt(0);
    }

    public void updateListView(List<EventInfo> list) {
        this.eventInfoList = list;
        notifyDataSetChanged();
    }
}
